#include "MainMenu.h"


#include "Character.h"
#include "ClippingPlane.h"

#include "GUIManager.h"
#include "Lua.h"
#include "GenericKeys.h"
#include "GameWorld.h"

MainMenu::MainMenu()
{
	//DataLoader->LoadData("map1Config");
	SoundPlay->Play(DataLoader->GetMusicName());
	
    // Load background image
    background.SetImageID("g_Menu_Background");

	// Reset the clipping plane
	ClipPlane->SetX(0);
	ClipPlane->SetY(0);

	// Create the user interface
	Lua->RunScript("Scripts\\UI\\DisableAll.lua");


	GUI->RegisterPanel(Key->GetPanelIDs()->Menu_Screen);
	GUI->ActivatePanel();

	



	// Rotating objects
	rotate_background.SetImageID("g_Menu_Background_rotate");
	rotate_background.ChangeX(50);
	rotate_background.ChangeY(50);

	rotate_background2.SetImageID("g_Menu_Background_rotate");
	rotate_background2.ChangeX(50);
	rotate_background2.ChangeY(50);

	rotate_background3.SetImageID("g_Menu_Background_rotate");
	rotate_background3.ChangeX(50);
	rotate_background3.ChangeY(50);

	rotation = 0.0f;
	rotation2 = 3.0f;
	rotation3 = 265.0f;
	
}

MainMenu::~MainMenu()
{
	//font.Destroy();
}
    
void MainMenu::handle_events()
{
    //While there's events to handle
    while( SDL_PollEvent( &event ) )
    {
		GUI->RegisterPanel(Key->GetPanelIDs()->Menu_Screen);
        // Mouse offsets
        int x = 0, y = 0;

        // On mouse motion
        if(event.type == SDL_MOUSEMOTION)
        {
                x = event.motion.x;
                y = event.motion.y;

				
				GUI->RegisterPanel(Key->GetPanelIDs()->Menu_Screen);
				int ID = GUI->GetButtonID(x, y);
				GUI->SwapButton(true);
				GUI->SwapButton(false, ID);
				

        }

        // Mouse press
        if(event.type == SDL_MOUSEBUTTONDOWN)
        {
            // Get the mouse offsets
            x = event.button.x;
            y = event.button.y;

            // Left button
            if(event.button.button == SDL_BUTTON_LEFT)
            {
				int buttonID = GUI->GetButtonID(x, y);

				if(buttonID == Key->GetPanelIDs()->Menu_NewGame)
					set_next_state(STATE_GAMEPLAY);

				if(buttonID == Key->GetPanelIDs()->Menu_Exit)
					set_next_state(STATE_EXIT);

				if(buttonID == Key->GetPanelIDs()->Menu_Editor)
					set_next_state(STATE_EDITOR);

				if(buttonID == Key->GetPanelIDs()->Menu_MapSelector)
					set_next_state(STATE_MAP_SELECTOR);
                
			}
        }    
                
        //If the user has Xed out the window
        if( event.type == SDL_QUIT )
        {
            //Quit the program
            set_next_state( STATE_EXIT );
        }
    }
}

void MainMenu::logic()
{
	GUI->Update();


	if(rotation > 360.0f)
		rotation = 0.0f;	
	else
		rotation += 1.0f;

	if(rotation2 > 360.0f)
		rotation2 = 0.0f;
	else
		rotation2 += 2.0f;

	if(rotation3 < 0.0f)
		rotation3 = 360.0f;
	else
		rotation3 -= 0.6f;
}

void MainMenu::render()
{
	
    // Render background first
        //background.applySurface();

		//rotate_background.applySurface(rotation);
		//rotate_background2.applySurface(rotation2);
		//rotate_background3.applySurface(rotation3);
		
		
		GUI->Render();
		
		
        
}